package betAndWin.gui;



import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import betAndWin.application.GameControl;


public class UIStartFrame extends JFrame{
	
	private static final String HOME_BACKGROUND_IMG_PATH = "img/corsa.png";

	private JLabel titolo = new JLabel("Created By: Antonio Panariello, Gianmarco Del Pozzo, Pasquale Caldarese");

	private BackgroundImgPanel pannello = new BackgroundImgPanel(HOME_BACKGROUND_IMG_PATH); 
	private JButton buttonStart = new JButton("Inizia Nuovo Gioco");
	private JButton buttonResume = new JButton("Riprendi Gioco");
	private JButton buttonExit = new JButton("Esci dal Gioco");
	
	private GameFrame gameFrame;

	public UIStartFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(800, 556);
		pannello.setOpaque(false);
		pannello.setLayout(null);

		titolo.setFont(new Font("Monotype Corsiva", Font.ITALIC, 16));
		titolo.setForeground(Color.RED);
		pannello.add(titolo);

		String[] value = {"Facile", "Medio", "Difficile"};
		final JComboBox difficulty = new JComboBox(value);
		difficulty.setSelectedIndex(1);

		titolo.setBounds(0, 0, 800, 20);
		difficulty.setBounds(250, 100, 150, 50);
		buttonStart.setBounds(170, 180, 150, 50);
		buttonResume.setBounds(100, 260, 150, 50);
		buttonExit.setBounds(30, 340, 150, 50);

		pannello.add(difficulty);
		pannello.add(buttonStart);
		pannello.add(buttonResume);
		pannello.add(buttonExit);

		this.setResizable(false);

		this.setContentPane(pannello);


		class startPlay implements ActionListener{
			public void actionPerformed(ActionEvent arg0) {
				if (gameFrame != null) {
					gameFrame.dispose();
					gameFrame.getGameControl().destroy();	// distruggo (stoppo) i thread dei cavalli di questo gc dato che non verra' piu' usato
				}
				
				int difficultySelectedIndex = difficulty.getSelectedIndex();
				double difficulty;
				switch (difficultySelectedIndex) {
				case 0:
					difficulty = GameControl.MODE_EASY;
					break;
				case 1:
					difficulty = GameControl.MODE_MEDIUM;
					break;
				case 2:
					difficulty = GameControl.MODE_HARD;
					break;
				default: difficulty = GameControl.MODE_MEDIUM;
				}
				
				final int NUMBER_OF_HORSES = 4;
				final GameControl gc = new GameControl(NUMBER_OF_HORSES, difficulty);
				gameFrame = new GameFrame(gc, UIStartFrame.this);
				gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				gameFrame.setLocationRelativeTo(null);		// centro la finestra
				
				UIStartFrame.this.setVisible(false);
				gameFrame.setVisible(true);
			}
		}
		
		class resumePlay implements ActionListener {
			public void actionPerformed(ActionEvent evt) {
				if (gameFrame != null) {
					UIStartFrame.this.setVisible(false);
					gameFrame.setVisible(true);
					gameFrame.setWelcomeSound(true);
					return;
				}
				
				final int NUMBER_OF_HORSES = 4;
				final GameControl gc = new GameControl(NUMBER_OF_HORSES, true);
				gameFrame = new GameFrame(gc, UIStartFrame.this);
				gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				gameFrame.setLocationRelativeTo(null);		// centro la finestra
				
				UIStartFrame.this.setVisible(false);
				gameFrame.setVisible(true);
			}
		}

		class exit implements ActionListener{
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		}

		buttonStart.addActionListener(new startPlay());
		buttonResume.addActionListener(new resumePlay());
		buttonExit.addActionListener(new exit());

	}

	public static void main(String[] v){
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				UIStartFrame startFrame = new UIStartFrame();
				startFrame.setVisible(true);
			}
		});
	}
}
